<?php
include_once('./inc/config.php');
if(!is_logged_in()) redirect('index.php');	


$mode = $_POST['mode'];
$actions = array();
switch($mode)
{

	case 'check':
		$actions = check_form();
	break;	

	default:
		die('Sorry, you have reached a wrong page.');
 	break;
	
}

/** 
 * Check Form
 *
 * @descp Validate User in the DB
 * @access public
 * @param  NA	 
 * @return void
 */
function check_form()
{
	global $dbobj, $proj_config;	
	
	$set = array();
	$actions = array();
	$errors = '';
	
	$txt_url 	= $_POST['txt_url'];
	
	if(empty($txt_url) || strlen($txt_url) < 5)
	{
	    $set['errors'] .= '<li>&raquo; Please enter a valid url (ex:www.example.com)</li>';
	}
	else if(!preg_match('/www./si', $txt_url))    
	{
	    $set['errors'] .= '<li>&raquo; Please enter a valid url (ex:www.example.com)</li>';
	}
	
    if(empty($set['errors']))        
	{
		//include cralwer script
		include_once(BASEPATH .'inc/crawler.php');
		$params = array();
		$params['website'] = $txt_url;
		$crawl = new eCrawler($params);
		$set = $crawl->run_script();
		$set['crawler_site'] = $txt_url;
		//seo recommnedations
		include_once(BASEPATH .'inc/keyword.generator.class.php');

		$params['content'] = (isset($set['body_data'][0]) && !empty($set['body_data'][0])) ? strip_tags($set['body_data'][0]) : '--'; //page content
		//set the length of keywords you like
		$params['min_word_length'] = 5;  //minimum length of single words
		$params['min_word_occur'] = 2;  //minimum occur of single words
		
		$params['min_2words_length'] = 3;  //minimum length of words for 2 word phrases
		$params['min_2words_phrase_length'] = 10; //minimum length of 2 word phrases
		$params['min_2words_phrase_occur'] = 2; //minimum occur of 2 words phrase
		
		$params['min_3words_length'] = 3;  //minimum length of words for 3 word phrases
		$params['min_3words_phrase_length'] = 10; //minimum length of 3 word phrases
		$params['min_3words_phrase_occur'] = 2; //minimum occur of 3 words phrase
		
		$keyword = new autokeyword($params, "iso-8859-1");
		$set['seo_keywords']['1_words_phrase'] = $keyword->parse_words();
		$set['seo_keywords']['2_words_phrase'] = $keyword->parse_2words();
		$set['seo_keywords']['3_words_phrase'] = $keyword->parse_3words();
		$set['seo_keywords']['all_words'] = $keyword->get_keywords();
		//if there is an error in the host or domain then display error and suggestion
		if(isset($set['curl_data']['last_op']['error']) && !empty($set['curl_data']['last_op']['error']))
		{
			 $set['errors'] .= '<li>&raquo; The domain <u>'.$txt_url.'</u> does not exist</li>';
			 $set['errors'] .= '<li>&raquo; Error information: '.$set['curl_data']['last_op']['error'].'</li>';
			 $set['errors'] .= '<li>&raquo; Please enter a valid url (ex:www.example.com)</li>';			 
			 $set['errors'] .= '<li>&raquo; Please check the domain in <a target="_blank" href="http://www.whois.net/whois/'.urlencode($txt_url).'">http://www.whois.net/whois/'.urlencode($txt_url).'</a></li>';
			 
			 $actions = $set;
			 echo '<br/>';
			/*************************************************************/
			//Do not chnage this part
			include(BASEPATH . 'views/messages.php');//include body part
			/************************************************************/	
		}
		else
		{	
			$actions = $set;
			/*************************************************************/
			//Do not chnage this part
			include(BASEPATH . 'views/seo_results.php');//include body part
			/************************************************************/	
		}
	}
	else
	{
		$actions = $set;
		echo '<br/>';
		/*************************************************************/
		//Do not chnage this part
		include(BASEPATH . 'views/messages.php');//include body part
		/************************************************************/	
	}
	
}
?>